package com.stone.sys.action;

/**
 * <pre>
 * 按钮查询（用于按钮查询及应用管理菜单对象按钮选择）
 * </pre>
 */

import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.para.IConstant;
import com.stone.sys.pojo.TMenu;
import com.stone.sys.service.MenuService;
import com.stone.utils.JsonUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.page.CustomPage;
import frm.ssh.util.page.Page;

public class MenuListAction extends BaseListAction {
	
	private String start;
	
	private String limit;
	
	// 选中菜单的Id
	private String id;

	private MenuService menuService;

    /** * 构造方法. */
    public MenuListAction() {
        this.entityClass = TMenu.class;
    }
	
	/**
	 * 用户给菜单分配按钮时的Ext.Grid查询
	 * 
	 * @return
	 * @throws AppException
	 */
	@SuppressWarnings("unchecked")
	public void getListExt() throws Exception {

		super.setPage((String.valueOf((Integer.parseInt(getStart()) / Integer.parseInt(getLimit()) + 1))));
		
		HashMap<String, Object> m = new HashMap<String, Object>();
	
		m.put("cols", "btn.id as id, btn.iconCls as iconCls, btn.title as title, btn.url as url, sortId as sortId");
		m.put("alias", "btn");
		m.put("join", "");

		m.put("conditions"," btn.btnId is not null and btn.parentID = " + id);
		m.put("clause", "");

		// 根据起始记录查询当前页记录
		List<HashMap<String,Object>> list = service.getPageListByDHql(Integer.parseInt(getStart()), Integer.parseInt(getLimit()), m, entityClass.getName());
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(list, jsonConfig);
 
        super.getResponse().getWriter().print("{result:" + json.toString() + ",totalCount:[" + service.getTotalByDHql((String) m.get("conditions"), null, this.entityClass.getName()+ " as btn") + "]}");
		
	}
	
	public String getStart() {
		return start == null ? "0" : start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getLimit() {
		return limit == null ? String.valueOf(CustomPage.PER_PAGE) : limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.service = menuService;
		this.menuService = menuService;
	}

	public HashMap<String, Object> setSearchCondition() {
		// TODO Auto-generated method stub
		return null;
	}
}
